// mircoApp/kaipin_laboratory/index/index.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    active: 0,
    child_active: 0,
    list: [],
    page: 1,
    total_page: 0,
    category_list: [],
    category_list_status: [{
        name: "全部",
        status: "5",
        id:0,
      },
      {
        name: "待开品",
        status: "0",
        id:1,
      },
      {
        name: "已出品",
        status: "1",
        id:2
      },
      {
        name: "出品中",
        status: "2",
        id:3
      },
      {
        name: "已完成",
        status: "3",
        id:4
      },
      {
        name: "开品失败",
        status: "-1",
        id:5
      },
    ],
    category_id: "",
    status: "",
    options:{},

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.options=options;
  },
  onSearch() {
    this.getData();
  },
  tab_status_handle(event) {
    let status = event.detail.name;
    let _json = {
      0:"",
      1:0,
      2:1,
      3:2,
      4:3,
      5:-1
    }
    this.data.status = _json[status];
    console.log(_json[status],'_json[status]===');
    this.setData({
      child_active: event.detail.name
    });
    this.getData();
  },
  tab_handle(event) {
    let category_id = event.detail.name;
    this.data.status = '';
    this.data.category_id = category_id == 0 ? '' : category_id;
    this.setData({
      child_active:0,
      active: event.detail.name
    });
    this.getData();
  },
  getCategory() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.kaipin-laboratory.frontend.product-plaza.get-category-list"),
      success: resdata => {
        let {
          result,
          data
        } = resdata.data;
        if (result) {
          let category_list = [{
            id: "",
            name: "全部"
          }, ...data];
          this.setData({
            show: true,
            category_list
          });
          if(this.data.options.category_id){
            let active = category_list.findIndex(item=>item.id==this.data.options.category_id);
            this.setData({active,category_id:this.data.options.category_id})
          }
          this.getData();
        }
      }
    })
  },
  goto(e) {
    let url = e.currentTarget.dataset.url;
    if (url == 'details') {
      let {
        id
      } = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: `/mircoApp/kaipin_laboratory/${url}/${url}?id=${id}`,
      })
      return;
    }
    wx.navigateTo({
      url: `/mircoApp/kaipin_laboratory/${url}/${url}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      status:"",
      keyword: "",
      child_active: 0
    })

    this.getCategory()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  getData(page = 1) {
    let data = {
      page,
      name: this.data.keyword,
      category_id: this.data.category_id,
      status: this.data.status
    }

    app._postNetWork({
      url: app.getNetAddresss("plugin.kaipin-laboratory.frontend.product-plaza.get-product-list"),
      data,
      success: resdata => {
        let {
          result,
          data
        } = resdata.data;
        if (result) {
          let list = data.current_page == 1 ? data.data : this.data.list.concat(data.data);
          this.setData({
            list
          });
          this.data.page = data.current_page;
          this.data.total_page = data.last_page;
        }
      }
    })
  },
  onChange(e) {
    this.setData({
      keyword: e.detail,
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.total_page) return;
    let page = this.data.page + 1;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})